var locationData = require('../../../staticData/locationKeyValue.json');
exports.followUpWithCustomersToday = (req, res) => {

    var restfulParams = req.routeInfo.restfulParams;

    var resultData = {};

    var todayStart = new Date(new Date().toLocaleDateString()).getTime() / 1000;

    var todayEnd = (new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000) / 1000;

    var u_id = restfulParams.u_id;

    var is_manager = restfulParams.is_manager;//是否是管理 0-否 1-是

    var cfr_uid_condition = '';

    var u_type = restfulParams.u_type;

    var u_osid = restfulParams.os_id;

    var salesArr = [];

    var userInStr = '';

    //查询员工
    function salesSearch(cb) {
        if (is_manager == 1 && (u_type == 1 || u_type == 2)) {

            //战略  查询战略伙伴下所有销售
            console.log("战略 || 代理");
            R.SEND_HTTP(
                req,
                res, {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_master_osid/" + u_osid,
                    method: 'POST',
                    data: {
                        fields: ['u_id'].join(",")
                    }
                },
                function (data) {

                    salesArr = data.result.data;

                    cb();

                }
            )
        } else if (is_manager == 1 && u_type == 3) {
            //销售主管  查询该销售主管下所有销售.包括自己。
            console.log("销售组长");
            R.SEND_HTTP(
                req,
                res, {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_leader/" + restfulParams.u_id,
                    method: 'POST',
                    data: {
                        fields: ['u_id'].join(",")
                    }
                },
                function (data) {

                    salesArr = data.result.data;

                    cb();

                }
            )
        } else if (is_manager == 0 && u_type == 3) {
            //普通销售
            console.log("普通销售");
            salesArr.push({
                u_id: u_id
            });
            cb();
        } else if (u_type == 0) {
            //超管
            console.log("超管");
            R.SEND_HTTP(
                req,
                res, {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url,
                    method: 'POST',
                    data: {
                        fields: ['u_id'].join(",")
                    }
                },
                function (data) {

                    salesArr = data.result.data;
                    cb();

                }
            )
        }
    }

    // 拼接in语句查询条件
    function fixInCondition(cb) {
        for (var i = 0; i < salesArr.length; i++) {
            userInStr += salesArr[i].u_id + ","
        }
        userInStr = userInStr.substring(0, userInStr.length - 1);

        if (userInStr == '') {
            userInStr = "null";
        }

        cfr_uid_condition = "/in/cfr_uid-" + userInStr;


        cb()
    }
    //今日跟进
    function followUpToday(cb) {

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.vFollowRecordUserSearch.Request_Api_Url  + cfr_uid_condition + "/between/" + todayStart + "-cfr_create_time-" + todayEnd,
                method: 'GET',
            },
            function (data) {

                resultData.followUpToday = data.result.data;

                cb();

            }
        )
    }

    //查询今天跟进的客户
    function queryForCustomerWhichHadBeenFollowedUpToday(cb) {

        F.SerialEach(resultData.followUpToday, function (item, index, callback) {

            //查询客户信息
            function customerInfo(eachCb) {

                R.SEND_HTTP(
                    req,
                    res,
                    {
                        url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/c_is_delete/0/c_id/" + item.cfr_cid,
                        method: 'POST',
                        data: {
                            fields: ['c_id', 'c_name', 'c_header_url', 'c_phone', 'c_campus_name', 'c_provinces', 'c_city', 'c_area', 'c_address', 'c_distribution_status', 'c_follow_status', 'c_evaluation', 'c_service_status', 'c_is_buy'].join(",")
                        }
                    },
                    function (data) {

                        var provinceId = data.result.data[0].c_provinces;
                        var cityId = data.result.data[0].c_city;
                        var areaId = data.result.data[0].c_area;
                        item.customerInfo = data.result.data;

                        item.customerInfo[0].provinceName = locationData[provinceId];
                        item.customerInfo[0].cityName = locationData[cityId];
                        item.customerInfo[0].areaName = locationData[areaId];
                        item.customerInfo[0].cfr_way = item.cfr_way;
                        item.customerInfo[0].cfr_create_time = item.cfr_create_time;
                        item.customerInfo[0].u_nickname = item.u_nickname;
                        item.customerInfo[0].c_follow_status = item.c_follow_status;

                        eachCb();

                    }
                )
            }

            function followRecordUser(eachCb) {

                R.SEND_HTTP(
                    req,
                    res,
                    {
                        url: CORE_APIConfig.coreAPI.vFollowRecordUserSearch.Request_Api_Url + "/count/1/cfr_cid/" + item.cfr_cid,
                        method: 'POST',
                        data: {
                            fields: ['cfr_way', 'cfr_create_time', 'u_nickname'].join(',')
                        }
                    },
                    function (data) {

                        item.followCount = data.result.count;

                        eachCb();

                    }
                )
            }


            F.Parallel([
                customerInfo,
                followRecordUser,
            ], () => {
                callback();
            })

        }, cb)

    }

    F.Serial([
        salesSearch,
        fixInCondition,
        followUpToday,
        queryForCustomerWhichHadBeenFollowedUpToday,
    ], () => {
        RES.successResponse(res, resultData);
    })


}